package com.woparker.kiosk.activity

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.woparker.kiosk.R
import com.woparker.kiosk.base.BaseActivity
import com.woparker.kiosk.utils.CommonUtils
import com.woparker.kiosk.utils.MultiLanguageUtil
import com.woparker.kiosk.utils.MultiLanguageUtil.LANGUAGE_CHINESE_TRADITIONAL
import com.woparker.kiosk.utils.MultiLanguageUtil.LANGUAGE_EN
import com.woparker.kiosk.view.PlateKey
import com.woparker.kiosk.view.Toasty


class MainActivity: BaseActivity() {
    private var plate_key: PlateKey? = null

    private var tv_search: TextView? = null

    private var input_close_iv: ImageView? = null

    private var but_find_plate: Button? = null

    private var language_bt:Button? = null

    private var beijing_time: TextView? = null

    private var mac: TextView? = null

    private var ip_state: TextView? = null

    private var versionNumber: TextView? = null

    private val REQUEST_CHANGE_WIFI_STATE: Int = 1
    override fun createView(): View {
        val view = LayoutInflater.from(this).inflate(R.layout.activity_main,null)
        return view
    }

    override fun initView() {
        requestPermission()
        plate_key = findViewById(R.id.plate_key)
        tv_search = findViewById(R.id.tv_search)
        input_close_iv = findViewById(R.id.input_close_iv)
        but_find_plate = findViewById(R.id.but_find_plate)
        language_bt = findViewById(R.id.language_bt)
        beijing_time = findViewById(R.id.beijing_time)
        mac = findViewById(R.id.mac)
        ip_state = findViewById(R.id.ip_state)
        plate_key?.setTextView(tv_search)
        plate_key?.onKeyClickListener = object : PlateKey.OnKeyClickListener {
            override fun onClick(isBack: Boolean, str: String) {
                var content = tv_search?.getText().toString().trim { it <= ' ' }
                if (isBack) {
                    if (!TextUtils.isEmpty(content)) {
                        val newStr = content.substring(0, content.length - 1)
                        tv_search?.text = newStr
                    }
                } else {
                    content = content + str
                    tv_search?.text = content
                }
            }
        }
        input_close_iv?.setOnClickListener {
            tv_search?.text = ""
        }

        but_find_plate?.setOnClickListener {
            val content = tv_search?.text
            content?.let {
                if (it.length < 2) {
                    showToast(Toasty.TYPE_NORMAL, getString(R.string.plate_tips))
                }
                else {
                    val intent = Intent(this, SearchResultsActivity::class.java)
                    intent.putExtra("carnumber", content)
                    startActivity(intent)
                    tv_search?.text = ""
                }
            }

        }
        language_bt?.setOnClickListener {
            val type = MultiLanguageUtil.getInt(MultiLanguageUtil.SAVE_LANGUAGE,LANGUAGE_CHINESE_TRADITIONAL)
            if(type == LANGUAGE_EN) {
                MultiLanguageUtil.getInstance().updateLanguage(LANGUAGE_CHINESE_TRADITIONAL)
            }
            else{
                MultiLanguageUtil.getInstance().updateLanguage(LANGUAGE_EN)
            }
            val intent = Intent(
                this,
                MainActivity::class.java
            )
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
            this.startActivity(intent)
            this.finish()
        }
        beijing_time?.text = CommonUtils.getCurrentFormattedTime()
        mac?.text = CommonUtils.getMacAddress(this)
        ip_state?.text = CommonUtils.getWifiIpAddress(this)
        versionNumber?.text = CommonUtils.getAppVersion(this)
    }

    override fun initData() {

    }


    fun requestPermission() {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CHANGE_WIFI_STATE)
            != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                arrayOf(android.Manifest.permission.CHANGE_WIFI_STATE),
                REQUEST_CHANGE_WIFI_STATE);
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CHANGE_WIFI_STATE) {
            if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {

            }
        }
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(MultiLanguageUtil.attachBaseContext(newBase))
    }
}